package longrise.phone.com.bjjt_jyb.bindinginformation;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.Global;
import com.longrise.android.ILSMsgListener;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.Constant.URLConstant;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.ApiImpI;
import longrise.phone.com.bjjt_jyb.Utils.CommontUtils;
import longrise.phone.com.bjjt_jyb.Utils.DialogUtils;
import longrise.phone.com.bjjt_jyb.Utils.PrefUtils;
import longrise.phone.com.bjjt_jyb.Utils.StringUtils;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.compensate.QRCodeView;


/**
 * 个人信息主界面
 */
public class PersonalInfoView extends LFView implements View.OnClickListener, ILSMsgListener {

    private Context mContext;

    private View mView;

    private LinearLayout nameLayout;

    private LinearLayout realNameLayout;

    private LinearLayout myCardLayout;

    private LinearLayout myCarLayout;

    private LinearLayout phoneLayout;

    private TextView phoneTV;

    private LinearLayout pwdLayout;

    private LinearLayout versionLayout;

    private TextView logoutTV;

    private BaseApplication application;
    //姓名
    private TextView nameTV;
    //真实姓名
    private TextView realNameTV;
    //绑定驾驶证
    private TextView bindCardTV;
    //绑定机动车
    private TextView bindCarTV;

    private EntityBean baseBean;//基础数据

    private EntityBean[] carBeans;//绑定的车辆信息

    private EntityBean cardBean;//驾驶证信息
    private TextView tv_banbennum;
    private TextView tv_bind;
    private TextView tv_bind2;
    private TextView tv_quyu;
    private LinearLayout quyu;
    private LinearLayout wdtj;
    private Dialog processDialog;
    private BaseApplication mApplication;
    private EntityBean[] beans;
    private TextView titleTV2;
    private int number;

    public PersonalInfoView(Context context, EntityBean bindBean,EntityBean[] bean,int number) {
        super(context);
        this.mContext = context;
        initInfos(bindBean);
        this.mApplication = (BaseApplication) context.getApplicationContext();
        this.beans=bean;
        this.number=number;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        if (null == mContext) {
            return;
        }
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_personalinfo, null);
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV2 = (TextView) mView.findViewById(R.id.tv_register);
        titleTV.setText(R.string.personal_info);
        titleTV2.setVisibility(View.VISIBLE);
        titleTV2.setText("我的二维码");
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        nameLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_name);
        realNameLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_realname);
        myCardLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_bindcard);
        myCarLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_bindcar);
        phoneLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_modifyphone);
        phoneTV = (TextView) mView.findViewById(R.id.pinfo_tv_phone);
        tv_bind = (TextView) mView.findViewById(R.id.tv_bind);
        tv_bind2 = (TextView) mView.findViewById(R.id.tv_bind2);
        tv_banbennum = (TextView) mView.findViewById(R.id.tv_banbennum);
        pwdLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_pwd);
        versionLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_version);
        wdtj = (LinearLayout) mView.findViewById(R.id.wdtj);
        quyu = (LinearLayout) mView.findViewById(R.id.quyu);
        logoutTV = (TextView) mView.findViewById(R.id.pinfo_tv_logout);
        //姓名
        nameTV = (TextView) mView.findViewById(R.id.pinfo_tv_name);
        //区域
        tv_quyu = (TextView) mView.findViewById(R.id.tv_quyu);
        //真实姓名
        realNameTV = (TextView) mView.findViewById(R.id.pinfo_tv_realname);
        //绑定驾驶证
        bindCardTV = (TextView) mView.findViewById(R.id.pinfo_tv_bindcard);
        //绑定机动车
        bindCarTV = (TextView) mView.findViewById(R.id.pinfo_tv_bindcar);
        TextView tv_wdtj = (TextView) mView.findViewById(R.id.tv_wdtj);
        tv_wdtj.setText(number+"人");
        tv_banbennum.setText(Global.getInstance().getVersionName());
        regEvent(true);
        this.addILSMsgListener(this);
        application = (BaseApplication) mContext.getApplicationContext();
        baseBean = application.getLoginInfo().getBean("userinfo");
        setData();

    }

    private void regEvent(boolean b) {
        if (null != nameLayout) {
            nameLayout.setOnClickListener(b ? this : null);
        }
        if (null != realNameLayout) {
            realNameLayout.setOnClickListener(b ? this : null);
        }
        if (null != myCardLayout) {
            myCardLayout.setOnClickListener(b ? this : null);
        }
        if (null != myCarLayout) {
            myCarLayout.setOnClickListener(b ? this : null);
        }
        if (null != phoneLayout) {
            phoneLayout.setOnClickListener(b ? this : null);
        }
        if (null != pwdLayout) {
            pwdLayout.setOnClickListener(b ? this : null);
        }
        if (null != versionLayout) {
            versionLayout.setOnClickListener(b ? this : null);
        }
        if (null != logoutTV) {
            logoutTV.setOnClickListener(b ? this : null);
        }
        if (null != quyu) {
            quyu.setOnClickListener(b ? this : null);
        }
        if (null != wdtj) {
            wdtj.setOnClickListener(b ? this : null);
        }
        if (null != titleTV2) {
            titleTV2.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pinfo_layout_name://修改姓名
                UiUtil.showToast(mContext,"暂不支持修改用户名");
                break;
            case R.id.pinfo_layout_realname://修改真实姓名
                UiUtil.showToast(mContext,"暂不支持修改真实姓名");
                break;
            case R.id.quyu:
                UiUtil.showToast(mContext,"暂不支持修改区域");
                break;
            case R.id.wdtj:
                if(beans!=null) {
                    MyCarView mycarview = new MyCarView(mContext,beans,number);
                    FrameworkManager.getInstance().showNewForm(mContext, mycarview);
                }
                else UiUtil.showToast(mContext,"您暂未推荐应用");
                break;
            case R.id.pinfo_layout_bindcard:
                /*if (null == cardBean) {//绑定驾驶证
                    BindDriveCardView dView = new BindDriveCardView(mContext, null);
                    showForm(dView);
                } else {//查看驾驶证
                    MyDriveCardView view = new MyDriveCardView(mContext, cardBean);
                    showForm(view);
                }*/
                UiUtil.showToast(mContext,"警号尚未分配");
                break;
            case R.id.pinfo_layout_bindcar:
                /*if (null == carBeans) {//绑定机动车
                    BindCarView cView = new BindCarView(mContext, null);
                    showForm(cView);
                } else {//查看车辆信息
                    MyCarView view = new MyCarView(mContext, carBeans);
                    showForm(view);
                }*/
                UiUtil.showToast(mContext,"所属大队尚未分配");
                break;
            case R.id.pinfo_layout_modifyphone://修改手机号
                ModifyPhoneView mView = new ModifyPhoneView(mContext);
                showForm(mView);
                break;
            case R.id.pinfo_layout_pwd://修改密码
                ModifyPwdView pView = new ModifyPwdView(mContext);
                showForm(pView);
                break;
            case R.id.pinfo_layout_version://当前版本
                VersionView versionView = new VersionView(mContext);
                showForm(versionView);
                break;
            case R.id.pinfo_tv_logout://退出登录
                logout();
                break;
            case R.id.tv_register://进入我的二维码
                QRCodeView versionView2 = new QRCodeView(mContext,1);
                showForm(versionView2);
                break;
            default:
                break;
        }
    }



    //解析信息
    private void initInfos(EntityBean bean) {
        if (null == bean) {
            return;
        }
        cardBean = bean.getBean("use");
        carBeans = bean.getBeans("car");
    }

    private void setData() {
        if (null == baseBean) {
            return;
        }
        //用户名
        String name = baseBean.getString("userflag");
        if (TextUtils.isEmpty(name)) {
            name = "";
        }
        nameTV.setText(name);
        //区域
        String area = baseBean.getString("area");
        if (TextUtils.isEmpty(area)) {
            area = "河南省";
        }
        tv_quyu.setText(area);
        //真实姓名
        String realName = baseBean.getString("name");
        if (TextUtils.isEmpty(realName)) {
            realName = "";
        }
        realNameTV.setText(realName);
        //手机号
        String phone = baseBean.getString("mobilephone");
        if (TextUtils.isEmpty(phone)) {
            phone = "";
            tv_bind.setText("绑定手机号");
            tv_bind2.setText("绑定");
        }else{
            tv_bind.setText("修改手机号");
            tv_bind2.setText("修改");
        }
        phoneTV.setText(phone);
        if (null == cardBean) {
            bindCardTV.setText("未绑定");
        } else {
            bindCardTV.setText("");
        }
        if (null == carBeans) {
            bindCarTV.setText("未绑定");
        } else {
            bindCarTV.setText("");
        }
    }

    /**
     * 退出登录
     */
    private void logout() {
        UiUtil.showDialog(mContext, "是否退出当前账号", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 清空sp的账号，密码，token，发全局消息
                PrefUtils.removeStringData(mContext, "loginInfo");
                PrefUtils.removeStringData(mContext, "loginUserName");
                PrefUtils.removeStringData(mContext, "loginPassword");
                //清除缓存
                application.setLoginInfo(null);
                application.setToken(null);
                UiUtil.showToast(mContext, "退出登录成功");
                //回到登录页
                if (FrameworkManager.getInstance().isPopForm()) {
                    FrameworkManager.getInstance().closePopForm();
                }
                FrameworkManager.getInstance().destroyAllForm();
            }
        });
    }

    /**
     * 查看个人信息
     */
    private void getUserInfo() {
        DialogUtils.showDialog(mContext);
        String userflag = application.getKckpName();
        String token = application.getToken();
        ApiImpI.getInstance(application).searchPersonInfo(userflag, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        EntityBean beans = (EntityBean) result;
                        String restate = beans.getString("restate");
                        if (TextUtils.equals(restate, "1")) {
                            baseBean = beans.getBean("data");
                            if (null != baseBean) {
                                setData();
                            }
                        }
                    }
                });
    }


    /**
     * 获取驾驶证信息
     */
    private void getDriveCard() {
        String userflag = application.getKckpName();
        String token = application.getToken();
        String owner = baseBean.getString("name");
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(application).getCarOwner(owner, userflag, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        EntityBean beans = (EntityBean) result;
                        String restate = beans.getString("restate");
                        String redes = beans.getString("redes");
                        if (TextUtils.equals(restate, "1")) {
                            cardBean = beans.getBean("data");
                            if (null != cardBean) {
                                MyDriveCardView view = new MyDriveCardView(mContext, cardBean);
                                showForm(view);
                                return;
                            }
                        }
                        UiUtil.showToast(mContext, redes);
                    }
                });
    }


    /**
     * 是否绑定信息
     */
    private void bindState() {
        try {
            String userId = baseBean.getString("id");
            String userflag = application.getKckpName();
            String token = application.getToken();
            DialogUtils.showDialog(mContext);
            ApiImpI.getInstance(application).bindState(userId, userflag, token,
                    new ApiImpI.RequestCallBack() {
                        @Override
                        public void onError(String error) {
                            DialogUtils.closeDialog();
                        }

                        @Override
                        public void onSuccess(Object result) {
                            DialogUtils.closeDialog();
                            EntityBean beans = (EntityBean) result;
                            EntityBean bean = beans.getBean("data");
                            if (null != bean) {
                                initInfos(bean);
                                setData();
                            }
                            //保存车辆和驾驶证的数据
                            String carInfo = com.longrise.LEAP.Base.IO.JSONSerializer.getInstance().Serialize(beans);
                            PrefUtils.setString(mContext, "carinfo", carInfo);
                            LSMsgCall(101, "carinfo");
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refresh() {
//        getUserInfo();
//        bindState();
    }

    @Override
    public Object onLSMsg(int i, Object... objects) {
        switch (i) {
            case 99://返回时刷新界面数据
                bindState();
                break;
            case 98://个人信息已修改
                baseBean = application.getLoginInfo().getBean("userinfo");
                setData();
                break;
        }
        return null;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        mView = null;
        regEvent(false);
        removeILSMsgListener(this);
        DialogUtils.destroyDialog();
    }
}
